package com.webapp.org.util.condatabase;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

import com.webapp.org.bean.CategoryBean;
import com.webapp.org.bean.DepartmentBean;
import com.webapp.org.bean.DesignationBean;
import com.webapp.org.bean.QualifiedTestBean;
import com.webapp.org.bean.ResultDateBean;
import com.webapp.org.bean.UserCreationBean;

public class HBConfigure
{
	private static final SessionFactory sessionFactory = buildSessionFactory();

	private static SessionFactory buildSessionFactory()
	{
		try
		{
			// Create the SessionFactory from hibernate.cfg.xml
			Configuration configuration = new Configuration();
			configuration.addPackage("com.webapp.org.bean");
			configuration.addAnnotatedClass(CategoryBean.class);
			configuration.addAnnotatedClass(QualifiedTestBean.class);
			configuration.addAnnotatedClass(DepartmentBean.class);
			configuration.addAnnotatedClass(DesignationBean.class);
			configuration.addAnnotatedClass(ResultDateBean.class);
			configuration.addAnnotatedClass(UserCreationBean.class);

			configuration.configure("resources/hibernate.cfg.xml");
			ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
					.applySettings(configuration.getProperties())
					.buildServiceRegistry();
			return configuration.buildSessionFactory(serviceRegistry);
		} catch (Throwable ex)
		{
			System.err.println("Initial SessionFactory creation failed." + ex);
			throw new ExceptionInInitializerError(ex);
		}
	}

	public static SessionFactory getSessionFactory()
	{
		/*
		 * Configuration configuration = new Configuration();
		 * configuration.configure(); ServiceRegistry serviceRegistry = new
		 * ServiceRegistryBuilder().applySettings(
		 * configuration.getProperties()).buildServiceRegistry(); SessionFactory
		 * sf = configuration.buildSessionFactory(serviceRegistry); return sf;
		 */
		return sessionFactory;
	}
}
